package com.example.wan1.view.project;

import com.example.wan1.data.BaseObserver;
import com.example.wan1.data.RetrofitUtil;
import com.example.wan1.data.RxSchedulers;
import com.example.wan1.model.ArticleBean;
import com.example.wan1.model.ProjectCategoryBean;
import com.example.wan1.model.common.CommonResList;

import java.util.List;

import javax.inject.Inject;

/***
 * 创建时间：2020/7/1 21:02
 * 创建人：10850
 * 功能描述：
 * 问题描述：当我们刚进入界面的请求和下拉刷新的请求一致时，该如何优雅的处理？
 */
public class ProjectPresenter implements
        ProjectContract.Presenter {

    @Inject
    ProjectContract.View mView;

    @Inject
    public ProjectPresenter() {

    }

    /***
     * 最新项目 刚进入
     */
    public void start() {
        getProjectNewList(0);
    }

    /***
     * 最新项目
     */
    @Override
    public void getProjectNewList(int page) {
        RetrofitUtil.getInstance().getProjectNewList(page)
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new BaseObserver<CommonResList<ArticleBean>>() {
                    @Override
                    public void onSuccess(CommonResList<ArticleBean> result) {
                        mView.onSuccess(result);
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        mView.onFailure(e.getMessage());
                    }
                });
    }

    /***
     * 项目列表
     */
    @Override
    public void getProjectList(int page, int cid) {
        RetrofitUtil.getInstance().getProjectList(page, cid)
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new BaseObserver<CommonResList<ArticleBean>>() {
                    @Override
                    public void onSuccess(CommonResList<ArticleBean> result) {
                        mView.onSuccess(result);
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        mView.onFailure(e.getMessage());
                    }
                });
    }

    /***
     * 分类
     */
    @Override
    public void getProjectCategory() {
        RetrofitUtil.getInstance().getProjectCategory()
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new BaseObserver<List<ProjectCategoryBean>>() {
                    @Override
                    public void onSuccess(List<ProjectCategoryBean> result) {
                        mView.onCategoryProject(result);
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        mView.onFailure(e.getMessage());
                    }
                });
    }

}
